# -*- coding: UTF-8 -*-

# Copyright (C) 2009 Daniele Favara.
# Written by Daniele Favara <daniele@zeroisp.com>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os, sys
import simplejson, urllib
from misc import unicode_urlencode

"""
ws.geonames.org/findNearbyWikipediaJSON
"""

class WikipediaCom(object):
    #apy_key=u'ABQIAAAATw59ltFDL-sx11P8_WP6NRQNZk9Iyf_aiMB_Ni0yIfD-UN3HZxR7uFcLuIhccLGY4eXtbrUeQggL_w'

    def __init__(self, url="http://ws.geonames.org/findNearbyWikipediaJSON"):
        """
        """
        self.url = url

    def _get(self, url):
        req = urllib.urlopen(url)
        print url
        ret = simplejson.load(req)
        return ret


    def search(self, lng,lat, **kw):
        params = {}
        params['lang'] = kw.get('lang', 'it')
        params['maxRows'] = kw.get('maxRows', 30)
        params['country'] = kw.get('country', 'all')
        if kw.get('postalcode', False):
            params['postalcode'] = kw.get('postalcode')
        params['radius'] = kw.get('radius', 30)
        params['lng'] = lng
        params['lat'] = lat

        url = "%s?%s"%(self.url,unicode_urlencode(params))
        return self._get(url)

if __name__ == '__main__':
    p = PanoramioCom()
    print p.search(-4.8669738, 33.6220506)
